package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eMallCategory;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.recharge.limit.BaseRMBMallLimit;
import com.yanqu.road.server.manger.HideMallMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

public abstract class BaseActivityMallLimit extends BaseRMBMallLimit {
    protected GamePlayer player;
    protected ActivityInfo activityInfo;

    public BaseActivityMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo);
        this.activityInfo = activityInfo;
    }

    protected abstract int activityCheck();

    @Override
    protected int pretreatment() {
        if(null != mallInfo &&
                mallInfo.getCategory() == eMallCategory.GoldTokeMall.getValue() &&
                mallInfo.getLimitBuyCount() == -1
        ){
            //代金币且不限购商品，不需要玩家登录状态
        }else {
            player = GamePlayerMgr.getOnlinePlayer(userId);
            if (null == player) {
                return GameErrorCode.E_MALL_LIMIT_BUY;
            }
        }
        return super.pretreatment();
    }

    @Override
    public LimitBuyResult checkMallLimit() {
        LimitBuyResult limitBuyResult = new LimitBuyResult();
        //预处理数据
        int resultCode = pretreatment();
        if(0 != resultCode){
            limitBuyResult.setRet(resultCode);
            return limitBuyResult;
        }
        resultCode = activityCheck();
        if(0 != resultCode){
            limitBuyResult.setRet(resultCode);
            return limitBuyResult;
        }
        //系统未解锁
        if(!isSystemUnLock()){
            limitBuyResult.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return limitBuyResult;
        }
        resultCode = logicCheck();
        if(0 != resultCode){
            limitBuyResult.setRet(resultCode);
            return limitBuyResult;
        }
        if(mallInfo.getCategory() == eMallCategory.RechargeGoods.getValue() && HideMallMgr.markIdIsShield(mallInfo.getMallId())){
            limitBuyResult.setRet(GameErrorCode.E_MALL_MARK_ID_SHIELD);
            return limitBuyResult;
        }
        //购买数量限制
        limitBuyResult = buyCountLimit();
        return limitBuyResult;
    }
}
